package com.iman.manjusri.web;

 
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.iman.common.support.Page;
import com.iman.manjusri.service.share.StudyResourceService;
import com.iman.manjusri.service.share.TagService;
import com.iman.manjusri.util.TagType;
import com.iman.manjusri.vo.share.ShareExpertVo;
import com.iman.manjusri.vo.share.StudyResourceVo;
import com.iman.manjusri.vo.share.TagVo;

 

/**
 * *************************************************************************
 * @文件名称: MyCalssController.java
 *
 * @包路径  : com.iman.Confucius.web.student 
 *				 
 * @版权所有:   TSW   科技有限公司 (C) 2014
 *
 * @类描述:   我的班级
 * 
 * @创建人:   wangxiaolei  
 *
 * @创建时间: 2014年12月18日 - 上午1:55:57 
 *
 * @修改记录:
   -----------------------------------------------------------------------------------------------
             时间						|		修改人		|		修改的方法		|		修改描述                                                                
   -----------------------------------------------------------------------------------------------
							|					|					|                                       
   ----------------------------------------------------------------------------------------------- 	
 
 **************************************************************************
 */


@Controller
@RequestMapping(value="/share/*")
public class ShareResouceController  extends BaseController {
	@Autowired
	private StudyResourceService resourceService;
	
	@Autowired
	private TagService tagService;
	
	 
	/**
	 * 资源库首页
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/resource")
	public ModelAndView index(HttpServletRequest request){
		ModelAndView view =  new ModelAndView("share/study_resource");	
		view.addObject("newestList", resourceService.newestResourceList(1, 6).getData());
		view.addObject("hotestList", resourceService.hotestResourceList(1, 6).getData());
		view.addObject("downRankList", resourceService.downRankResourceList(1, 8).getData());
		Page<ShareExpertVo> shareExpertPage = resourceService.shareExpertList(1, 5);
		view.addObject("shareExpertList",shareExpertPage.getData());
		view.addObject("shareExpertNext", String.valueOf(shareExpertPage.hasNextPage()));
		Map<String,Object> modeltag = new HashMap<String,Object>();
		modeltag.put("type",TagType.RESOURCE.toString() );				
		List<TagVo>  hottags= tagService.getHotTags(modeltag);
		view.addObject("hottags",hottags);
		putResourceSrc(view);		
		return view;
	}
	
	
	/**
	 * 获取分享达人列表
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/resource/expertListAjax/{pageNum}")
	public @ResponseBody Map shareExpertListAjax(HttpServletRequest request,@PathVariable int pageNum){
		Map<String,Object> map = new HashMap<String,Object>();
		Page<ShareExpertVo> shareExpertPage = resourceService.shareExpertList(pageNum, 5);
		map.put("shareExpertList",shareExpertPage.getData());
		map.put("shareExpertNext", String.valueOf(shareExpertPage.hasNextPage()));
		return map;
	}
	/**
	 * 浏览量+1
	 * @param resource_id
	 * @return
	 */
	@RequestMapping(value="/resource/resourceViewAjax/{resource_id}")
	public @ResponseBody int resourceViewAjax(@PathVariable long resource_id){
		this.resourceService.incrViewCnt(resource_id);
		return 0;
	}
	/**
	 * 验证下载积分
	 * @return
	 */
	@RequestMapping("/resource/valid_integral/{resource_id}")
	public @ResponseBody Map validDownIntegralAjax(@PathVariable long resource_id,HttpServletRequest request){
		Map<String,Object> result = new HashMap<String,Object>();
		StudyResourceVo resource = this.resourceService.resourceSimpleDetail(resource_id);
		int integral = resource.getNeed_integral();
		long userId = this.getUserIdInSession(request);
		if(integral > 0){
			
			ShareExpertVo user = this.resourceService.userIntegral(userId);
			int user_integral = user.getUser_integral();
			if(user_integral - integral < 0){
				result.put("can", false);
				result.put("msg", "您的积分不够哦~");
			} else {
				result.put("can", true);
				Map<String,Object> params = new HashMap<String,Object>();
				params.put("resource_id", resource_id);	
				params.put("user_id", userId);	
				params.put("integral", integral);	
				this.resourceService.incrDownCnt(resource_id);//下载量+1
				this.resourceService.recordDown(resource_id, userId, integral);//记录下载历史
				this.resourceService.reduceDownIntegral(userId, user_integral);//用户积分减少
			}
		} else {
			result.put("can", true);
			this.resourceService.incrDownCnt(resource_id);
			this.resourceService.recordDown(resource_id, userId, integral);
			result.put("msg", "");
		}		
		return result;
	}
	/**
	 * 获取评论人的头像
	 * @param user_id
	 * @param comment_id
	 * @return
	 */
	@RequestMapping("/resource/getHeadImg/{user_id}/{comment_id}")
	public @ResponseBody Map getHeadImgAjax(@PathVariable long user_id,@PathVariable String comment_id,HttpServletRequest request){
		Map<String,Object> result = new HashMap<String,Object>();		
		if(!StringUtils.equals(comment_id, "null"))
			result.put("comment_id", comment_id);
		if(user_id == 0)
			user_id = this.getUserIdInSession(request);
		ShareExpertVo user = this.resourceService.commentUserHead(user_id);
		result.put("himg", user!=null?user.getUser_himg():"");
		result.put("uid", user_id);
		result.put("uname", user.getUser_lnm());
		return result;
	}
	/**
	 * 
	 *  Function:
	 *  功能说明：显示资源详情页面
	 *	 使用说明：
	 *  @author  wangxiaolei  DateTime 2014年12月16日 下午2:59:30
	 *	返回类型: ModelAndView    
	 *  @param request
	 *  @param menu
	 *  @return
	 */	
	@RequestMapping(value="/resource/d/{file_id}")
	public ModelAndView resourceDetail(HttpServletRequest request,
			@Valid @PathVariable long file_id	) throws Exception { 	 
		ModelAndView view =  new ModelAndView("share/drm_zyzx_xx");
		StudyResourceVo resource = this.resourceService.resourceDetail(file_id);
		view.addObject("resource", resource);
		Page<ShareExpertVo> shareExpertPage = resourceService.shareExpertList(1, 5);
		view.addObject("shareExpertList",shareExpertPage.getData());
		view.addObject("shareExpertNext", String.valueOf(shareExpertPage.hasNextPage()));
		view.addObject("downUserList", resourceService.downUserList(resource.getResource_id(), 0, 5));
		putResourceSrc(view);		
		return view;
	} 
	
	@RequestMapping(value="/resource/tag/{tagid}")
	public ModelAndView resourceByTag(HttpServletRequest request,@PathVariable String tagid) throws Exception { 
		ModelAndView view =  new ModelAndView("share/drm_zyzx_tab");	
		
		Map<String, Object> model = new HashMap<String, Object>();
		int pagesize =10; 
		int pageno = 0; 
	 
		if(request.getParameter("pageNo")==null ||request.getParameter("pageNo").equals("") ){
			pageno =  1;	
		}
		else{
			pageno = Integer.parseInt(request.getParameter("pageNo"));
		}
		model.put("tagid", tagid);		 
		Page<StudyResourceVo> vos = resourceService.resourceListByTagId( pageno,pagesize,model);
		view.addObject("pageData", vos);		
		
		TagVo tag = tagService.getById(model);
		view.addObject("tag", tag);		
		Map<String,Object> modeltag = new HashMap<String,Object>();
		modeltag.put("type",TagType.RESOURCE.toString() );				
		List<TagVo>  hottags= tagService.getHotTags(modeltag);
		view.addObject("hottags",hottags);
		putResourceSrc(  view);		
		return view;
	} 

	@RequestMapping(value="/resource/more/{resourceType}")
	public ModelAndView resourceMore(HttpServletRequest request,@PathVariable String resourceType) throws Exception { 
		ModelAndView view =  new ModelAndView("share/drm_zyzx_more");	
		
		Map<String, Object> model = new HashMap<String, Object>();
		int pagesize =10; 
		int pageno = 0;
		//String resourceType = request.getParameter("type");
		view.addObject("resourcetype", resourceType);
		if(request.getParameter("pageNo")==null ||request.getParameter("pageNo").equals("") ){
			pageno =  1;	
		}
		else{
			pageno = Integer.parseInt(request.getParameter("pageNo"));
		}
		if(resourceType.equals("download"))
		{			
			view.addObject("pageData", resourceService.downRankResourceList( pageno,pagesize));
		}
		else if(resourceType.equals("upload"))
		{
			view.addObject("pageData", resourceService.newestResourceList( pageno,pagesize));
		}
		else if(resourceType.equals("hot"))
		{
			Page<StudyResourceVo> vos = resourceService.hotestResourceList( pageno,pagesize);
			view.addObject("pageData", vos);			
		}
		Map<String,Object> modeltag = new HashMap<String,Object>();
		modeltag.put("type",TagType.RESOURCE.toString() );				
		List<TagVo>  hottags= tagService.getHotTags(modeltag);
		view.addObject("hottags",hottags);
		putResourceSrc(  view);
		
		return view;
	}  
}
